﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.IO;
using ClearingRestartingAutomaton.Base;
using ClearingRestartingAutomaton.Common;

namespace ClearingRestartingAutomaton
{
    public partial class ClearingAutomatonForm : Form
    {
        public ClearingAutomatonForm()
        {
            InitializeComponent();
        }

        public Automaton ReadInstructions(bool showMessages)
        {
            Automaton automaton = new Automaton();
            string[] lines = this.instructionTextBox.Lines;
            for (int i = 0; i < lines.Length; ++i)
            {
                string line = lines[i];

                if (String.IsNullOrWhiteSpace(line) || line.Trim().StartsWith("//"))
                {
                    continue;
                }
                
                Instruction[] instructions = RegexPatterns.ParseInstructions(line);

                if (instructions != null && instructions.Length > 0)
                {
                    foreach (Instruction instruction in instructions)
                    {
                        automaton.Instructions.Add(instruction);
                    }
                }
                else
                {
                    if (showMessages)
                    {
                        MessageBox.Show("Instruction on the " + i.ToString() +
                            ". row does not match the instruction pattern.", "Warning");
                    }
                    return null;
                }
            }
            return automaton;
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Do yo really want to clear the document?", "Confirmation",
                MessageBoxButtons.YesNo) == DialogResult.Yes)
                this.instructionTextBox.Clear();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult openFileDialogResult = this.openFileDialog.ShowDialog();
            if (openFileDialogResult == DialogResult.OK)
            {
                TextReader textReader = new StreamReader(this.openFileDialog.FileName);
                this.instructionTextBox.Text = textReader.ReadToEnd();
                textReader.Close();
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult saveFileDialogResult = this.saveFileDialog.ShowDialog();
            if (saveFileDialogResult == DialogResult.OK)
            {
                TextWriter textWriter = new StreamWriter(this.saveFileDialog.FileName);
                textWriter.Write(this.instructionTextBox.Text);
                textWriter.Close();
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void reduceGenerateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Automaton automaton = ReadInstructions(true);
            if (automaton != null)
            {
                ReduceGenerateForm reduceGenerateForm = new ReduceGenerateForm(automaton);
                reduceGenerateForm.Show();
            }
        }

        private void learnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LearningForm learningForm = new LearningForm();
            learningForm.Show();
        }

        private void inferToolStripMenuItem_Click(object sender, EventArgs e)
        {
            InferenceForm inferenceForm = new InferenceForm();
            inferenceForm.Show();
        }

        private void sampleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Automaton automaton = ReadInstructions(true);
            LanguageForm languageForm = new LanguageForm(automaton);
            languageForm.Show();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string info =
                "The main goal of this application is to investigate the properties of clearing restarting automata." +
                Environment.NewLine + Environment.NewLine +
                "2012 Peter Cerno" +
                Environment.NewLine +
                "petercerno@gmail.com" +
                Environment.NewLine +
                Environment.NewLine +
                "The development of this application was was partially supported by the Grant Agency of Charles University under Grant-No. 272111/A-INF/MFF and by the Czech Science Foundation under Grant-No. P103/10/0783 and Grant-No. P202/10/1333.";
            InformationForm infoForm = new InformationForm(info);
            infoForm.ShowDialog();
        }

        private void ClearingAutomatonForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
                e.Cancel = MessageBox.Show("Do you really want to close the Clearing Restarting Automaton application?",
                    "Confirmation", MessageBoxButtons.YesNo) == DialogResult.No;
        }
    }
}
